package com.shinbada.modules.sys.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.shinbada.common.ResultCode;
import com.shinbada.common.dict.Dict;
import com.shinbada.common.dict.EnumDict;
import com.shinbada.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 排班计划类型
 *
 * @author initcap
 * @date Created in 2020/12/1 15:28.
 */
@AllArgsConstructor
@Getter
@Dict("scheduling-type")
public enum SchedulingType implements EnumDict<String> {

    work("班"),
    holiday("休");

    private String text;

    /**
     * 用来解决当使用@RequestBody进行接收参数时无法序列化问题
     *
     * @param text 前端传过来的值
     * @return 找到的枚举
     */
    @JsonCreator
    public static SchedulingType fromText(String text) {
        for (SchedulingType state : SchedulingType.values()) {
            if (state.getValue().equals(text)) {
                return state;
            }
        }
        throw new ServiceException(ResultCode.SCHEDULING_TYPE_NOT_EXIST);
    }

    @Override
    public String getValue() {
        return name();
    }

}
